<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Profile extends Controller_User_Template{	
	
	public function before() {
		parent::before();
		if($this->auto_render) {
			$this->template->sidebar = View::factory('user/profile/sidebar');
		}
	}
	public function action_index()
	{
		if($loggedin = Auth::instance()->get_user())
		{
			$user_id = $loggedin->id;
			$profile = ORM::factory('user')
					->where('id', '=', $user_id)
					->find();
			$address = ORM::factory('address')
					->where('user_id', '=', $user_id)
					->find_all();
			
			$view = View::factory('user/profile/index');
			$view->set('addresses', $address);
			$view->set('profile', $profile);
			$this->template->content = $view;
			
		}
		else
		{
			$view = View::factory('user/login');
			$this->template->content = $view;
		}
	}
	
	public function action_orders()
	{
		if($loggedin = Auth::instance()->get_user())
		{
			$user_id = $loggedin->id;
			$orders = ORM::factory('order')
					->where('user_id', '=', $user_id)
					->find_all();
			
			$view = View::factory('user/profile/orders');
			$view->set('orders', $orders);
			$this->template->content = $view;
			
		}
		else
		{
			$view = View::factory('user/login');
			$this->template->content = $view;
		}
	}
	
	public function action_order()
	{
		
		if($loggedin = Auth::instance()->get_user())
		{
			$user_id = $loggedin->id;
			$order_id = $this->request->param('id');
			$order = ORM::factory('order', $order_id);
			
			$view = View::factory('user/profile/order');
			$view->set('order', $order);
			$this->template->content = $view;
		}
		else
		{
			$view = View::factory('user/login');
			$this->template->content = $view;
		}
	}
	
}